<?php

hc_core_ClassLoader::load('halo_IView');

abstract class halo_AbstractView implements halo_IView {
    private static $DEFAULT_CONTENT_TYPE = 'text/html;charset=ISO-8859-1';
    protected $viewName;
    protected $url;
    protected $additionalConfigurationKeys = array();
    protected $additionalConfiguration = array();
    public function __construct($viewName = null, $url = null) {
        $this->viewName = $viewName;
        $this->url = $url;
    }
    public function getAdditionalConfigurationKeys() {
        return $this->additionalConfigurationKeys;
    }
    public function setAdditionalConfiguration($key, $value) {
        $this->additionalConfiguration[$key] = $value;
    }
    public function existsAdditionalConfiguration($key) {
        return array_key_exists($key, $this->additionalConfiguration);
    }
    public function getAdditionalConfiguration($key) {
        return $this->existsAdditionalConfiguration($key) ?
            $this->additionalConfiguration[$key] : null;
    }
    final public function render(array $model, halo_HttpRequest $httpRequest, halo_HttpResponse $httpResponse) {
        /*
        hc_core_ClassLoader::load('halo_SystemViewHelperMapping');
        $additionalViewHelperMappings = array(
            new halo_SystemViewHelperMapping($httpRequest, $httpResponse)
        );
        */
        return $this->doRender($model, $httpRequest, $httpResponse);
    }
    abstract public function doRender(array $model, halo_HttpRequest $httpRequest, halo_HttpResponse $httpResponse);
    protected function getViewHelperMappings(halo_HttpRequest $httpRequest, halo_HttpResponse $httpResponse) {

        
        $context = halo_DispatcherUtil::GET_CONTEXT($httpRequest);

        $viewHelperMappings = $context->findStonesByImplementation(
            'halo_IViewHelperMapping'
        );

        $viewHelperMappingFactories = $context->findStonesByImplementation(
            'halo_IViewHelperMappingFactory'
        );

        foreach ( $viewHelperMappingFactories as $viewHelperMappingFactory ) {
            $viewHelperMappings[] = $viewHelperMappingFactory->createViewHelperMapping($httpRequest, $httpResponse);
        }

        return $viewHelperMappings;

    }

    public function getConfiguredHelpersMap() {

        $map = array();
        
        $helpers = $this->getAdditionalConfiguration('helpers');

        if ( $helpers !== null ) {
            foreach ( explode(',', $helpers) as $pair ) {
                $parts = explode('=', $pair);
                if ( count($parts) > 1 ) {
                    $variableName = $parts[0];
                    $helperName = $parts[1];
                } else {
                    $variableName = $helperName = $parts[0];
                }
                $map[$variableName] = $helperName;
            }
        }

        foreach ( array('uri', 'httpRequest', 'httpResponse') as $defaultHelper ) {
            if ( ! isset($map[$defaultHelper]) ) {
                $map[$defaultHelper] = $defaultHelper;
            }
        }

        return $map;

    }

}

?>
